import 'package:date_format/date_format.dart';
import 'package:timeago/timeago.dart' as timeago;

/// 时间工具
class ZtDateUtil {
  // 获取返回数据中的today 没有则默认当前时区的今天
  DateTime getToday() {
    return DateTime.now();
  }

  /// datetime时间转多久之前
  static String formatDateTimeToTimeago(DateTime? dateTime) {
    return dateTime != null ? timeago.format(dateTime, locale: 'zh_CN') : '-';
  }

  /// str时间转多久之前
  static String formatStrToTimeago(String? strTime) {
    if (strTime == null) {
      return '-';
    }
    // String ztime = '${strTime.substring(0, 19)}Z';
    DateTime zdatetime = DateTime.parse(strTime);
    DateTime now = ZtDateUtil().getToday();
    int agoDayLimit = 3;
    if (now.difference(zdatetime).inDays < agoDayLimit) {
      return timeago.format(zdatetime, locale: 'zh_CN');
    } else {
      return formatDate(
          DateTime.parse(strTime), ['yyyy', '-', 'mm', '-', 'dd']);
    }
  }

  /// String类型的时间 转化为 Str yyyy-mm-dd
  static String formatStrTimeToStrDate(String? time) {
    return time != null
        ? formatDate(DateTime.parse(time), ['yyyy', '-', 'mm', '-', 'dd'])
        : '-';
  }

  /// datetime类型的时间 转化为 Str类型的默认为yyyy-mm-dd
  static String formatDateTimeToStrDate(DateTime? time,
      {List<String>? formats}) {
    return time != null
        ? formatDate(time, formats ?? ['yyyy', '-', 'mm', '-', 'dd'])
        : '-';
  }
}
